<?php
 	header("content-type:text/plain;charset=utf-8");
	session_start();
	require "../modules/conn.php";
	$uid=$_SESSION["uid"];
	$uname=$_SESSION["uname"];
	$title=$_REQUEST["title"];
	$subtitle=$_REQUEST["subtitle"];
	$cond=$_REQUEST["cond"];
	$curr=$_REQUEST["curr"];
	$price=$_REQUEST["price"];
	$payment=implode(",",$_REQUEST["payment"]);
	$desc=$_REQUEST["description"];

	$sql="insert into t_sell(title,subtitle,cond,curr,price,payment,s_des,pub_date,uid) values('$title','$subtitle','$cond','$curr',$price,'$payment','$desc',now(),$uid)";
	echo $sql;

	// 将内容插入数据库，除图片
		if($uid&&$uname&&$title&&$subtitle&&$cond&&$curr&&$price&&$payment&&$desc){
			$result=mysqli_query($conn,$sql);
			$sid=mysqli_insert_id($conn);
			if($result){
				$_SESSION["sid"]=$sid;
				header("location:../details.html");
			}else{
				echo $uid;
			}
		}else{
			header("location:../sell.html");
		}
		


// 方法一
	// foreach ($_FILES as $file) {
	// 	if (!empty($file["name"])) {
	// 		$Img="../upload/".rand(1111,9999).$file["name"];
	// 		move_uploaded_file($file['tmp_name'], $Img);
	// 		$Img=str_replace("..", "/twoproject", $Img);	
	// 		$sqlimg="insert into t_image(imgcontent,s_img_id) values('$Img',$sid)";
	// 		$resultimg=mysqli_query($conn,$sqlimg);
	// 		if($resultimg){
	// 			echo "111111";
	// 		}else{
	// 			echo "2222";
	// 		}
	// 	}
	// }

	// 将图片插入数据库
	// 方法二
	foreach ($_FILES as $file) {
		if (!empty($file["name"])) {
			// 1、打开临时存储的图片文件
			$fp = fopen($file["tmp_name"], "rb");
			// 2、读取文件总字节数
			$size = filesize($file["tmp_name"]);
			// 3、读取文件内容
			$content = fread($fp, $size);
			// 4、将字符串内容做无格式处理
			$image = addslashes($content);
			$type = $file["type"];
			$sqlimg = "insert into t_image (imgcontent, imgtype, s_img_id) values ('$image', '$type', $sid)";
			$resultimg = mysqli_query($conn, $sqlimg);
			if($resultimg){
				echo 'ok';
			}else{
				echo 'error';
			}
		}
	}

?>